package com.xyzyzglxt.system.service.impl;

import java.util.*;

import com.xyzyzglxt.common.core.domain.TreeSelect;
import com.xyzyzglxt.common.core.domain.entity.SysEventType;
import com.xyzyzglxt.common.core.domain.model.LoginUser;
import com.xyzyzglxt.common.core.domain.vo.NumNowdayVo;
import com.xyzyzglxt.common.utils.DateUtils;
import com.xyzyzglxt.common.utils.FastDFSUtils;
import com.xyzyzglxt.common.utils.SecurityUtils;
import com.xyzyzglxt.common.core.domain.vo.EventCheckVo;
import com.xyzyzglxt.common.core.domain.vo.EventStateVo;
import com.xyzyzglxt.system.mapper.SysEventTypeMapper;
import com.xyzyzglxt.system.mapper.SysUserEventMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xyzyzglxt.system.mapper.SysEventMapper;
import com.xyzyzglxt.system.domain.SysEvent;
import com.xyzyzglxt.system.service.ISysEventService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动信息Service业务层处理
 *
 * @author xyzyzglxt
 * @date 2022-01-17
 */
@Service
public class SysEventServiceImpl implements ISysEventService {
    @Autowired
    private SysEventMapper sysEventMapper;

    @Autowired
    private SysEventTypeMapper sysEventTypeMapper;

    @Autowired
    private SysUserEventMapper sysUserEventMapper;

    /**
     * 查询活动信息
     *
     * @param eventId 活动信息主键
     * @return 活动信息
     */
    @Override
    public SysEvent selectSysEventByEventId(Long eventId) {
        SysEvent sysEvent = sysEventMapper.selectSysEventByEventId(eventId);
        SysEventType sysEventType = sysEventTypeMapper.selectSysEventTypeByTypeId(sysEvent.getEventTypeId());
        sysEvent.setSysEventType(sysEventType);
        return sysEvent;
    }

    /**
     * 查询活动信息列表
     *
     * @param sysEvent 活动信息
     * @return 活动信息
     */
    @Override
    public List<SysEvent> selectSysEventList(SysEvent sysEvent) {
        SysEvent sysEventone = creatEventRole(sysEvent);
        List<SysEvent> sysEvents = sysEventMapper.selectSysEventList(sysEventone);
        for (SysEvent one : sysEvents) {
            SysEventType sysEventType = sysEventTypeMapper.selectSysEventTypeByTypeId(one.getEventTypeId());
            one.setSysEventType(sysEventType);
            // 未开始
            if (DateUtils.getMs(one.getEventStart(), new Date()) > 0) {
                one.setEventStatus("0");
            }
            // 正在进行
            if (DateUtils.getMs(one.getEventStart(), new Date()) <= 0) {
                one.setEventStatus("1");
            }
            // 已结束
            if (DateUtils.getMs(new Date(), one.getEventEnd()) >= 0) {
                one.setEventStatus("2");
            }
            // 过期
            if (DateUtils.getMs(new Date(), one.getEventEnd()) >= 86400000) {
                one.setEventStatus("3");
            }
        }
        return sysEvents;
    }

    /**
     * 查询已审核的活动信息
     *
     * @param sysEvent
     * @return
     */
    @Override
    public List<SysEvent> selectCheckSysEventList(SysEvent sysEvent) {
        sysEvent.setEventCheck("1");
        List<SysEvent> sysEvents = sysEventMapper.selectSysEventList(sysEvent);
        for (SysEvent one : sysEvents) {
            SysEventType sysEventType = sysEventTypeMapper.selectSysEventTypeByTypeId(one.getEventTypeId());
            one.setSysEventType(sysEventType);
            // 正在进行
            if (DateUtils.getMs(one.getEventStart(), new Date()) <= 0) {
                one.setEventStatus("1");
            }
            // 已结束
            if (DateUtils.getMs(new Date(), one.getEventEnd()) >= 0) {
                one.setEventStatus("2");
            }
            // 过期
            if (DateUtils.getMs(new Date(), one.getEventEnd()) >= 86400000) {
                one.setEventStatus("3");
            }
        }
        return sysEvents;
    }

    /**
     * 新增活动信息
     *
     * @param sysEvent 活动信息
     * @return 结果
     */
    @Override
    public int insertSysEvent(SysEvent sysEvent) {
        //招募单位不能定义审核状态
        // LoginUser loginUser = SecurityUtils.getLoginUser();
        // sysEvent.setEventUserId(loginUser.getUserId());
        // List<SysRole> sysRoles = loginUser.getUser().getRoles();
        // for(SysRole sysRole : sysRoles){
        //      Long roleId = sysRole.getRoleId();
        //     if(roleId == 2){
        //         sysEvent.setEventCheck("0");
        //     }
        // }
        sysEvent.setEventCheck("0");
        sysEvent.setEventRdpeoples(0L);
        return sysEventMapper.insertSysEvent(sysEvent);
    }

    /**
     * 修改活动信息
     *
     * @param sysEvent 活动信息
     * @return 结果
     */
    @Override
    public int updateSysEvent(SysEvent sysEvent) {
        int checkRole = checkRole(sysEvent.getEventId());
        if (checkRole != 0) {
            return checkRole;
        }
        // 活动审核后不允许修改
        SysEvent sysEventone = sysEventMapper.selectSysEventByEventId(sysEvent.getEventId());
        if (sysEventone.getEventCheck().equals("1")) {
            return -1;
        }
        // 修改后审核状态重新回到未审核
        sysEvent.setEventCheck("0");
        return sysEventMapper.updateSysEvent(sysEvent);
    }

    /**
     * 批量删除活动信息
     *
     * @param eventIds 需要删除的活动信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysEventByEventIds(Long[] eventIds) {
        for (Long eventId : eventIds) {
            int checkRole = checkRole(eventId);
            if (checkRole != 0) {
                return checkRole;
            }
        }
        // 删除活动的所有报名信息
        for (Long eventId : eventIds) {
            SysEvent sysEvent = sysEventMapper.selectSysEventByEventId(eventId);
            // 若审核通过
            if (sysEvent.getEventCheck().equals("1")) {
                // 活动已开始不允许删除
                if (!sysEvent.getEventStatus().equals("0")) {
                    return -1;
                }
                // 删除活动的所有报名信息
                sysUserEventMapper.removeUserEvents(eventId);
            }
        }
        return sysEventMapper.deleteSysEventByEventIds(eventIds);
    }

    /**
     * 删除活动信息信息
     *
     * @param eventId 活动信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysEventByEventId(Long eventId) {
        int checkRole = checkRole(eventId);
        if (checkRole != 0) {
            return checkRole;
        }
        SysEvent sysEvent = sysEventMapper.selectSysEventByEventId(eventId);
        // 若审核通过
        if (sysEvent.getEventCheck().equals("1")) {
            // 活动已开始不允许删除
            if (!sysEvent.getEventStatus().equals("0")) {
                return -1;
            }
            // 删除活动的所有报名信息
            sysUserEventMapper.removeUserEvents(eventId);
        }
        return sysEventMapper.deleteSysEventByEventId(eventId);
    }

    /**
     * 审核不通过
     *
     * @param sysEvent
     * @return
     */
    @Override
    public int FailSysEvent(SysEvent sysEvent) {

        int checkTime = checkTime(sysEvent.getEventId());
        if (checkTime != 0) {
            return checkTime;
        }
        sysEvent.setEventCheck("2");
        return sysEventMapper.updateSysEvent(sysEvent);
    }

    /**
     * 审核通过
     *
     * @param sysEvent
     * @return
     */
    @Override
    public int SuccessSysEvent(SysEvent sysEvent) {
        int checkTime = checkTime(sysEvent.getEventId());
        if (checkTime != 0) {
            return checkTime;
        }
        sysEvent.setEventCheck("1");
        return sysEventMapper.updateSysEvent(sysEvent);
    }

    /**
     * 获取活动下拉树列表
     */
    @Override
    public List<TreeSelect> buildEventTreeSelect(List<SysEvent> sysEvents) {
        List<TreeSelect> treeSelectList = new ArrayList<TreeSelect>();
        List<SysEventType> sysEventTypeList = sysEventTypeMapper.selectSysEventTypeList(new SysEventType());
        for (SysEventType sysEventType : sysEventTypeList) {
            TreeSelect treeSelect = new TreeSelect();
            treeSelect.setId(sysEventType.getTypeId());
            treeSelect.setLabel(sysEventType.getTypeName());
            List<TreeSelect> treeSelectListone = new ArrayList<TreeSelect>();
            for (SysEvent sysEvent : sysEvents) {
                if (Objects.equals(sysEvent.getEventTypeId(), sysEventType.getTypeId())) {
                    TreeSelect treeSelectone = new TreeSelect();
                    treeSelectone.setId(sysEvent.getEventId());
                    treeSelectone.setLabel(sysEvent.getEventName());
                    treeSelectListone.add(treeSelectone);
                }
            }
            treeSelect.setChildren(treeSelectListone);
            treeSelectList.add(treeSelect);
        }
        return treeSelectList;
    }

    /**
     * 上传(更新)活动图片
     *
     * @param url
     * @param eventId
     * @return
     */
    @Override
    public int updateSysEventImg(String url, Long eventId) {
        SysEvent sysEvent = sysEventMapper.selectSysEventByEventId(eventId);
        if (sysEvent.getEventImg() != null) {
            String groupName = "group1/";
            String remoteFileName = sysEvent.getEventImg().split(groupName)[1];
            groupName = groupName.replace("/", "");
            FastDFSUtils.deleteFile(groupName, remoteFileName);
        }
        sysEvent.setEventImg(url);
        return sysEventMapper.updateSysEvent(sysEvent);
    }

    /**
     * 招募单位则只显示他所管理的活动
     *
     * @param sysEvent
     * @return
     */
    private SysEvent creatEventRole(SysEvent sysEvent) {
        Long roleId = SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleId();
        if (roleId == 3) {
            sysEvent.setEventUserId(SecurityUtils.getUserId());
        }
        return sysEvent;
    }

    /**
     * 招募单位只能处理自己的发布的活动
     *
     * @return
     */
    private int checkRole(Long eventId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long roleId = loginUser.getUser().getRoles().get(0).getRoleId();
        if (roleId == 3) {
            if (sysEventMapper.selectSysEventByEventId(eventId).getEventUserId() != SecurityUtils.getUserId()) {
                // 招募单位只能处理自己的发布的活动
                return -10;
            }
        }
        return 0;
    }

    /**
     * 审核时间需在活动开始之前
     * 仅管理员审核
     *
     * @return
     */
    private int checkTime(Long eventId) {
        Long roleId = SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleId();
        // 仅管理员审核
        if (roleId != 1) {
            return -12;
        }
        SysEvent sysEvent = sysEventMapper.selectSysEventByEventId(eventId);

        if(!sysEvent.getEventCheck().equals("0")){
            return -14;
        }
        // 审核时间需在活动开始之前
        if (DateUtils.getMs(sysEvent.getEventStart(), new Date()) < 0) {
            return -13;
        }
        return -0;
    }

    /**
     * 返回活动状态及名称
     *
     * @return
     */
    @Override
    public List<EventStateVo> eventState() {
        List<EventStateVo> eventStateVos = new ArrayList<EventStateVo>() {{
            add(new EventStateVo("0", "未开始"));
            add(new EventStateVo("1", "进行中"));
            add(new EventStateVo("2", "已结束"));
            add(new EventStateVo("3", "已过期"));
        }};
        return eventStateVos;
    }

    /**
     * 返回活动审核名称
     *
     * @return
     */
    @Override
    public List<EventCheckVo> eventCheckInfo() {
        List<EventCheckVo> eventCheckVos = new ArrayList<EventCheckVo>() {{
            add(new EventCheckVo("0", "未审核"));
            add(new EventCheckVo("1", "审核通过"));
            add(new EventCheckVo("2", "未通过"));
        }};
        return eventCheckVos;
    }

    /**
     * 当天时间活动数and报名人数
     *
     * @return
     */
    @Override
    public List<NumNowdayVo> findNumByNowday() {
        Date date = DateUtils.getNowDate();
        List<NumNowdayVo> numNowdayVos = new ArrayList<NumNowdayVo>();
        for (int i = 0; i < 7; i++) {
            if(i != 0){
                Calendar instance = Calendar.getInstance();
                instance.setTime(date);
                instance.add(Calendar.DATE,-1);
                date = instance.getTime();
            }
            NumNowdayVo numNowdayVo = new NumNowdayVo();
            numNowdayVo.setSData(DateUtils.dateTime(date));
            Long[] eidByNowday = sysEventMapper.findEidByNowday(date);
            int eventNum = 0;
            int vteerNumNowday = 0;
            if(eidByNowday.length != 0){
                eventNum = eidByNowday.length;
                // 获取志愿者人数
                vteerNumNowday = sysEventMapper.findVteerNumNowday(eidByNowday);
            }
            numNowdayVo.setEventNum(eventNum);
            numNowdayVo.setVolunteerNum(vteerNumNowday);

            numNowdayVos.add(numNowdayVo);
        }
        return numNowdayVos;
    }
}
